/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 *
 * @version 1.0
 */
package shop.controller.front.product;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.services.front.product.ProductService;
import shop.services.front.product.bean.Product;
import shop.services.front.spec.SpecService;
import shop.services.front.spec.bean.Spec;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @类名称：ProductController
 * @创建人：Ltz
 * @创建时间：2017-09-20 下午15:50:43
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/front/product/")
public class ProductController extends FrontBaseController<Product> {
    @Resource
    private ProductService productService;
    @Resource
    private SpecService specService;

    @Override
    public Services<Product> getService() {
        return productService;
    }


    /**
     * 跳转到商品列表页面
     *
     * @return
     */
    @RequestMapping("toProductList")
    public String toProductList() {
        return "/front/catalog/catalogList";
    }

    /**
     * 跳转到商品详情页面
     *
     * @return
     */
    @RequestMapping("toProductDetail")
    public String toProductDetail() {
        return "/front/product/productDetail";
    }

    /**
     * 跳转到购物车页面
     *
     * @return
     */
    @RequestMapping("toCartList")
    public String toCartList() {
        return "/front/product/shoppingCart";
    }

    /**
     * 查询商品列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value="selectProductList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectProductList(Product product){
        JSONResult jsonResult = new JSONResult();
        List<Product> productList = productService.selectProductList(product);
        for (int i = 0; i < productList.size(); i++) {
            Spec spec = new Spec();
            spec.setProductID(productList.get(i).getId());//获取rsList的第i+1个元素
            List<Spec> specList = specService.selectList(spec);
            productList.get(i).setSpecs(specList);//把字段specs存入商品
        }
        jsonResult.setData(productList);
        return jsonResult;
    }

    /**
     * 查询商品详情
     *
     * @param product
     * @return
     */
    @RequestMapping(value = "selectProductDetail")
    @ResponseBody
    public JSONResult selectProductDetail(Product product) {
        JSONResult jsonResult = new JSONResult();
        Product productInfo = productService.selectOne(product);
        //查询商品规格列表
        Spec spec = new Spec();
        spec.setProductID(product.getId());
        List<Spec> specs = specService.selectList(spec);
        productInfo.setSpecs(specs);
        jsonResult.setData(productInfo);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询订单详情（商品详情入口）
     *
     * @param spec
     * @return
     */
    @RequestMapping(value = "selectOrderDetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectOrderDetail(Spec spec) {
        jsonResult = new JSONResult();
        spec = specService.selectOne(spec);
        jsonResult.setData(spec);
        return jsonResult;
    }

    @Override
    public JSONResult insertJson(HttpServletRequest request, Product product) throws Exception {
        return null;
    }

    @Override
    public JSONResult deleteJson(HttpServletRequest request, Product product) throws Exception {
        return null;
    }
}
